using System;
using System.Diagnostics;
using jSLApp.Web.Workflow;

namespace jSLApp.Web
{
    public sealed class EmailTraceListener : TraceListener
    {
        private string _currentMessage=string.Empty;

        public override void Write(string message)
        {
            _currentMessage += message;
        }

        public override void WriteLine(string message)
        {
            var mes = _currentMessage+message;

            if (!mes.Contains(".jWorkflowException"))
            {
#if !DEBUG
                Do(mes);
#endif
            }
            _currentMessage = string.Empty;
        }

        private void Do(string message)
        {
            try
            {
                var text = message;
                using (var db = new DB())
                {
                    MainService.SendMailToAllSuperVisors(db, text);
                }
            }
            catch
            {

            }
        }
    }
}